<?php

namespace Controller;

use Model\GameData\RepData;
use Config\Cfg;
use Model\GameData\RepClientData;
use Handler\UserHandler;
use Handler\AdminHandler;
use GatewayWorker\Lib\Gateway;

/**
 * 基础控制器
 *
 * @author Administrator
 *        
 */
class BaseController {
	/**
	 */
	function __construct() {
	}
	/**
	 */
	function __destruct() {
	}
	/**
	 * 发送HTTP消息
	 *
	 * @param unknown $clientId        	
	 * @param unknown $result        	
	 * @param unknown $data        	
	 */
	protected function mb_OutHttpMessage($clientId, $result, $data) {
		if ($clientId == null || $clientId == '')
			return;
		$repData = new RepData ();
		$repData->m_Result = $result;
		$repData->m_Data = $data;
		$repData->m_Sign = md5 ( $result . $data . Cfg::MSG_MD5 );
		Gateway::sendToClient ( $clientId, json_encode ( $repData ) );
	}
	/**
	 * 发送TEXT消息
	 *
	 * @param string $clientId        	
	 * @param string $cmd        	
	 * @param string $result        	
	 * @param string $data        	
	 */
	protected function mb_OutTextMessage($clientId, $recNo, $result, $data) {
		if ($clientId == null || $clientId == '')
			return;
		$repData = new RepClientData ();
		$repData->m_RecNo = $recNo;
		$repData->m_Result = $result;
		$repData->m_Data = $data;
		$repData->m_Sign = md5 ( $recNo . $result . $data . Cfg::MSG_MD5 );
		Gateway::sendToClient ( $clientId, json_encode ( $repData ) );
	}
	/**
	 * 发送Web消息
	 * @param unknown $clientId
	 * @param unknown $message
	 */
	protected function mb_OutWebMessage($clientId, $message) {
		if ($clientId == null || $clientId == '')
			return;
		Gateway::sendToClient ( $clientId, $message );
	}
	/**
	 *
	 * @param unknown $actCode        	
	 * @param unknown $actMsg        	
	 * @param unknown $actDelay        	
	 * @return unknown[]
	 */
	protected function mb_EncodeDialogData($actCode, $actMsg) {
		return array (
				'actcode' => $actCode,
				'actmsg' => $actMsg 
		);
	}
	/**
	 * 检测用户时候登录中
	 *
	 * @param unknown $clientId        	
	 * @param unknown $recNo        	
	 * @param unknown $token        	
	 * @return boolean|\Model\User|\Handler\NULL
	 */
	protected function mb_CheckUserIsLogining($clientId, $recNo, $token) {
		$user = UserHandler::mb_GetInstance ()->mb_GetUserForToken ( $token );
		if (isset ( $user ) == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return false;
		}
		return $user;
	}
	/**
	 * 检测管理员时候登录中
	 *
	 * @param unknown $clientId        	
	 * @param unknown $recNo        	
	 * @param unknown $token        	
	 * @return boolean|\Handler\NULL|\Model\Admin
	 */
	protected function mb_CheckAdminIsLogining($clientId, $recNo, $token) {
		$admin = AdminHandler::mb_GetInstance ()->mb_GetAdminForToken ( $token );
		if (isset ( $admin ) == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return false;
		}
		return $admin;
	}
}